package fr.mywiki.business.resources;

import java.text.MessageFormat;
import java.util.Locale;
import java.util.ResourceBundle;

import javax.servlet.http.HttpServletRequest;

import fr.mywiki.business.log.LogManager;

public class ResourceManager {
	private ResourceManager() {}
	
	/* The names/paths of the different resource bundles. */
	private static final String BUNDLE_ACTIONS = "bundle.actions";
	private static final String BUNDLE_ERRORS = "bundle.errors";
	private static final String BUNDLE_MESSAGES = "bundle.messages";
	private static final String BUNDLE_SPECIFIC = "bundle.specific";
	private static final String BUNDLE_IMAGES = "bundle.images";
	
	/**
	 * Returns the action resource coresponding to the key given in parameter.
	 * 
	 * @param key the key for the desired action.
	 * @param locale the locale of the connected user.
	 * @return a <code>String</code> object
	 */
	public static String getAction(String a_key, Locale a_locale) {
		return ResourceBundle.getBundle(BUNDLE_ACTIONS, a_locale).getString(a_key);
	}
	
	/**
	 * Returns the error resource coresponding to the key given in parameter.
	 * 
	 * @param key the key for the desired error.
	 * @param locale the locale of the connected user.
	 * @return a <code>String</code> object
	 */
	public static String getError(String a_key, Locale a_locale) {
		return ResourceBundle.getBundle(BUNDLE_ERRORS, a_locale).getString(a_key);
	}
	
	/**
	 * Returns the message resource coresponding to the key given in parameter.
	 * 
	 * @param key the key for the desired message.
	 * @param locale the locale of the connected user.
	 * @return a <code>String</code> object
	 */
	public static String getMessage(String key, Locale locale) {
		return ResourceBundle.getBundle(BUNDLE_MESSAGES, locale).getString(key);
	}
	
	/**
	 * Returns the message resource coresponding to the key given in parameter.
	 * 
	 * @param key the key for the desired message.
	 * @param locale the locale of the connected user.
	 * @param params the parameters.
	 * @return a <code>String</code> object
	 */
	public static String getMessage(String key, Locale locale,
			Object[] params) {
		try {
			String trad = ResourceBundle.getBundle(
					"bundle.messages", locale).getString(key);
			trad = trad.replaceAll("'","''");
			return MessageFormat.format(trad, params);
		} catch (RuntimeException e) {
			LogManager.println("Missing resource : " + key);
		}
		return key;
	}
	
	/**
	 * Returns the specific resource coresponding to the key given in parameter.
	 * 
	 * @param key the key for the desired message.
	 * @param locale the locale of the connected user.
	 * @return a <code>String</code> object
	 */
	public static String getSpecific(String key, Locale locale) {
		return ResourceBundle.getBundle(BUNDLE_SPECIFIC, locale).getString(key);
	}
	
	/**
	 * Returns the specific resource coresponding to the key given in parameter.
	 * 
	 * @param key the key for the desired image.
	 * @param locale the locale of the connected user.
	 * @return a <code>String</code> object
	 */
	public static String getImage(String key, Locale locale) {
		return ResourceBundle.getBundle(BUNDLE_IMAGES, locale).getString(key);
	}
	
	public static Locale getLocale(HttpServletRequest request) {
		return request.getLocale();
	}
}
